
let files = require.context('@/pages',true,/\.vue$/)
// console.log(files)
// console.log(files.keys())

let repeatRoutes = files.keys().map(path=>{

    let currentComponent = files(path).default
    // console.log(currentComponent.meta)
    let {name,meta,redirect}=currentComponent

    let regExp =/(\/index.vue|\.vue)/

    path = path.substring(1).replace(regExp,'')
    // console.log(path)
 
    return {
        path:path ? path :'/pages',
        component:()=>import('@/pages'+path),
        meta:meta ??{},
        name:name ?? path.substring(1).split('/').join('-').toLowerCase(),
        redirect:redirect ?? null,
    }
})

// console.log(repeatRoutes)

function converTree(repeatRoutes){
    repeatRoutes=[...repeatRoutes]
    //存放指针的最终容器
    let treeRoutes = []
    repeatRoutes.forEach(route=>{
        route.path=route.path.toLowerCase()
        let routePathArr = route.path.substring(1).split('/')
        // console.log(routePathArr)

        route.id = routePathArr.join('-')

        if(routePathArr.length>1){
            let pathArr = [...routePathArr]
            //去掉自身，剩余既是pid
            pathArr.pop()
            let pid = pathArr.join('-')   
            route.pid =pid
        }
    })
    // console.log(repeatRoutes)
    //转换成树形数据
    repeatRoutes.forEach((route)=>{
        route.path = route.path.toLowerCase()

        if(route.path==="/page404"){
            route.path="*"
        }

        let parent = repeatRoutes.find(v=>v.id===route.pid)
        if(parent){
            if(!parent.children){
                parent.children=[]
            }
            // console.log(route.path)
            route.path = route.path.substring(1).split('/').pop()
            // console.log(route.path)
            parent.children.push(route)
        }else{
            treeRoutes.push(route)
        }
    })
    return treeRoutes
}

let res = converTree(repeatRoutes)
console.log(res)
export default [...res]
